package com.globant;

import java.util.List;

import com.globant.exceptions.EmailAlreadyRegisteredException;
import com.globant.exceptions.InvalidEmailException;
import com.globant.exceptions.LengthException;
import com.globant.profy.model.User;


public class UserService {
	
private UserDao userDao;
private EmailValidator emailValidator;
private LengthValidator lengthValidator;

public LengthValidator getLengthValidator() {
	return lengthValidator;
}

public void setLengthValidator(LengthValidator lengthValidator) {
	this.lengthValidator = lengthValidator;
}

public UserService(UserDao userDao) {
	super();
	this.userDao = userDao;
}

public EmailValidator getEmailValidator() {
	return emailValidator;
}

public void setEmailValidator(EmailValidator emailValidator) {
	this.emailValidator = emailValidator;
}

public UserService() {
	super();
}

public void createUser(User user) throws InvalidEmailException, EmailAlreadyRegisteredException, LengthException{
	
	String email2Validate=user.getEmail();
	String name2Validate=user.getName();
	String company2Validate=user.getCompany();
	String jobTitle2Validate=user.getJobTitle();
	String password2Validate= user.getPassword();
	String experience2Validate=user.getExperience();
	
	
	if(!lengthValidator.validateLength(1, name2Validate, 50)){
		throw new LengthException("NAME");
	}

	if(!lengthValidator.validateLength(1, company2Validate, 50)){
		throw new LengthException("COMPANY");
	}
	
	if(!lengthValidator.validateLength(1, jobTitle2Validate, 50)){
		throw new LengthException("JOB TITLE");
	}
	
	if (!lengthValidator.validateLength(3, password2Validate, 12)) {
		throw new LengthException("PASSWORD");
	}
	
	if(!lengthValidator.validateLength(1, experience2Validate, 500)){
		throw new LengthException("EXPERIENCE");
	}
	
	if (!emailValidator.isValid(email2Validate)) {
		throw new InvalidEmailException();
	}
	
	if (!emailValidator.isAvailable(email2Validate)){
		throw new EmailAlreadyRegisteredException();
	}
	
	userDao.createUser(user);
//	try {	userDao.createUser(user);	}
//	catch (Exception e){
//		//throw new EmailAlreadyRegisteredException();
//		
//	}
}
		
public void updateUser(User user) throws LengthException, InvalidEmailException, EmailAlreadyRegisteredException{
	String experience2Validate=user.getExperience();
	
	
	if(!lengthValidator.validateLength(1, experience2Validate, 500)){
		throw new LengthException("EXPERIENCE");
	}
		
	try{
		userDao.updateUser(user);
	}catch (Exception e){
		e.printStackTrace();
	}
	
}
public void deleteUser(User user){
	userDao.deleteUser(user);
	
}
public List<User> getAllUsers(){
	List<User> allUsers= userDao.getAllUsers();
	return allUsers;
}
public User getUserByEmail(String email){
	User user=userDao.getUserByEmail(email);
	return user;
	
}

public UserDao getUserDao() {
	return userDao;
}

public void setUserDao(UserDao userDao) {
	this.userDao = userDao;
}

public List<User> getUserByCriteria(String text){
	List<User> searchedUsers= userDao.getUserByCriteria(text);
	return searchedUsers;
}	

}
